var User = function() {
    return User.fn.init();
}



// Declare
User.fn = User.prototype = {

	init:function(){
		this.sendMessage();
	},

	start:function(){
		this.initMenu();
		this.initFormMessage();
		this.initFooterSub();
	},

	/**
	 * Send mail
	 */
	sendMessage: function(){
		var _this = this;

		$('.message .panel-body button.btn').click(function(event){
			event.preventDefault();
			var $btn = $(this)
			var $form = $btn.closest('form');
			var json = $form.serialize();
			$btn.attr('disable', true);

			postAjax(webroot + 'messages/sendMessage', json, function(resp){
				$btn.attr('disable', false);
				if(resp.code == 'success'){
					$form.find('input').val('').attr('value', '');
					$form.find('textarea').val('').attr('value', '').html('');
				}
				var $msg = $('<div class="alert alert-'+resp.code+'"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Thành Công:</strong><br> '+ resp.msg+'</p></div>');
				$form.prepend($msg);

				setTimeout(function(){
					$msg.slideUp(function(){ $(this).remove() });
				}, 3000);

			}, 'json', true);
		});
	},
	/**
	 * Menu
	 */
	initMenu: function() {
		$(".main .left .menu ul ").css({display: "none"}); // Opera Fix

		$(".main .left .menu li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(300); /*.slideDown(300);  .animate({ width: "70%"}, 1500);*/
		},function(){
			$ul = $(this).find('ul:first');
			$ul.slideUp(300, function(){$ul.css({visibility: "hidden"})});
		});


		$(".fix-row .menu").css({display: "none"}); // Opera Fix
		$(".fix-row .fix-menu").hover(function(){
			$ul = $(this).find('ul:first');
			$ul.css({visibility: "visible",display: "none"}).slideDown(300, function(){$ul.css({visibility: "visible"})}); /*.slideDown(300);  .animate({ width: "70%"}, 1500);*/
		},function(){
			$ul = $(this).find('ul:first');
			$ul.slideUp(100, function(){$ul.css({visibility: "hidden"})});
		});
	},

	/**
	 * Message
	 */
	initFormMessage:function (){
		$('.message .panel-title a').click(function(event){
			event.preventDefault();
			var $btn = $(this);
			var $panel_body = $btn.closest('.panel-heading').next();
			var $msg = $panel_body.closest('.message');

			if( $btn.find('i.pull-right').hasClass('fa-chevron-up') ){
				$msg.animate({width:"310px"}, function(){
					$panel_body.show(500);
					$btn.find('i.pull-right').removeClass('fa-chevron-up').addClass('fa-chevron-down');
				});
			} else {
				$panel_body.hide(500, function(){
					$msg.animate({width:"220px"});
					$btn.find('i.pull-right').removeClass('fa-chevron-down').addClass('fa-chevron-up');
				});

			}
		});
	},

	/**
	 * Footer-sup
	 */
	initFooterSub:function(){
		$("#scroller").simplyScroll({ autoMode: 'loop'});
	},

	/**
	 * Google map
	 */
	googleMap:function(lat, lng, address) {
		var path = [ [lat,lng] ];

		map = new GMaps({
	        el: '#map',
	        zoom: 16,
	        lat: lat,
	        lng: lng
		});

		map.addMarker({
	        lat: lat,
	        lng: lng,
	        infoWindow: {
		        content: '<p>'+ address +'</p>'
		    },
	        title: address,
			draggable: false,
		});

		// show infoWindow
		map.markers[0].infoWindow.open(map, map.markers[0]);
	},
}

var user = new User();
